package com.dingding.web.order.action;

import java.util.List;

import javax.annotation.Resource;

import com.dingding.common.core.base.dto.FacadeResult;
import com.dingding.common.core.beans.UserLogin;
import com.dingding.common.core.util.ToolUtil;
import com.dingding.common.core.util.tools.wechat.WechatTool;
import com.dingding.facde.activity.service.ICommonActivityFacade;
import com.dingding.facde.share.IShareActivityFacade;
import com.dingding.order.dto.myorder.SpeCusOrdInfoResult;
import com.dingding.order.service.special.ISpecialCarOrderFacade;
import com.dingding.web.base.action.BaseAction;

/**
 * @Description:微信订单aciton
 * @author fengshuonan
 * @date 2016年9月23日 下午2:25:12
 * @version 叮叮搭车
 * @since 2.4
 */
@SuppressWarnings("serial")
public class WechatOrderAction extends BaseAction{

	@Resource(name = "specialCarOrderFacade")
	private ISpecialCarOrderFacade specialCarOrderFacade;
	
	@Resource(name = "shareActivityFacade")
	private IShareActivityFacade shareActivityFacade;
	
	@Resource(name = "commonActivityFacade")
	private ICommonActivityFacade commonActivityFacade;
	
	
	/**
	 * @Description:登录之前的action（用于微信免登陆）
	 * @author fengshuonan
	 * @return String
	 * @since 2.4
	 */
	public String startToLogin(){
		
		String code = super.getPara("code");			//为网页授权API回调时返回，用于获取用户的openId
		if(ToolUtil.isBlank(code)){
			return ERROR;
		}
		
		//通过返回的code去获取用户的openId
		String openId = WechatTool.getOpenIDByCode(code);
		if(ToolUtil.isBlank(openId)){
			return ERROR;
		}
		
		FacadeResult<Boolean> facade = this.commonActivityFacade.isOpenIdInDb(openId);
		if(facade.getCode() != 200){
			return ERROR;
		}
		
		//如果该openId没有存在于库中
		if(facade.getObject() == false){
			super.redirect("/dingding-web/driverOrder/tellphone.html");
		}else{
			//获取用户登录信息
			FacadeResult<UserLogin> facadeLogin = this.commonActivityFacade.getUserLoginByOpenId(openId);
			if(facadeLogin.getObject() == null){
				super.redirect("/dingding-web/driverOrder/tellphone.html");
			}else{
				//把当前用户的登录id放到session
				super.setSessionUserId(facadeLogin.getObject().getUid());
				super.redirect("/dingding-web/driverOrder/driver-order.html");
			}
		}
		return NONE;
	}
	
	
	/**
	 * @Description:获取用户的订单列表
	 * @author fengshuonan
	 * @since 2.4
	 */
	public String getUserSpecialOrders(){
		
		String userId = super.getSessionUserId();
		
		if(userId == null){
			//从session中获取当前用户的id如果为空，说明还没有登录
			super.writeResponseJsonp(400, "请重新登录");//session失效
		
		}else{
			//如果session中有当前访问的用户id
			List<SpeCusOrdInfoResult> cusOrders = this.specialCarOrderFacade.getCusSpeOrderByUserId(userId);
			
			if(cusOrders == null || cusOrders.size() == 0){
				//如果当前用户没有订单
				super.writeResponseJsonp(201,"订单列表为空");
			}else{
				//如果当前用户有订单
				super.writeResponseJsonp(cusOrders);
			}
		}
		
		return NONE;
	}
	
	/**
	 * @Description:微信端，查看订单的用户登录接口
	 * @author fengshuonan
	 * @since 2.4
	 */
	public String userLogin(){
		
		String telephone = super.getPara("telephone");
		String messageCode = super.getPara("messageCode");
		
		if(ToolUtil.validateParas(telephone,messageCode)){
			super.writeResponseJsonp(400, "请求参数为空");
			return NONE;
		}
		
		//判断验证码是否正确
		FacadeResult<Boolean> facade = this.commonActivityFacade.validateTeleCode(telephone, messageCode);
		if(facade.getCode() != 200){  //如果验证码超时
			super.writeFacade(facade);
			return NONE;
		}else{
			if(facade.getObject() == false){
				//如果验证码错误
				super.writeResponseJsonp(401,"验证码错误");
				
			}else{
				//如果验证码正确，则获取该用户的用户id
				FacadeResult<UserLogin> facadeLogin = this.commonActivityFacade.getUserLoginByTele(telephone);
				if(facadeLogin.getObject() == null){
					super.writeResponseJsonp(404, "用户未注册");
				}else{
					//把userId存入到session中
					super.setSessionUserId(facadeLogin.getObject().getUid());
					super.writeResponseJsonp(200,"请求成功");
				}
			}
		}
		return NONE;
	}
	
	/**
	 * @Description:取消专车订单
	 * @author fengshuonan
	 * @since 2.4
	 */
	public String cancelOrder(){
		
		String orderId = super.getPara("orderId");
		if(ToolUtil.isBlank(orderId)){
			super.writeResponseJsonp(400, "请求数据为空");
			return NONE;
		}
		boolean flag = this.specialCarOrderFacade.cancelSpecialOrder(orderId);
		if(flag == true){
			super.writeResponseJsonp(200, "取消订单成功");
		}else{
			super.writeResponseJsonp(500, "服务器异常");
		}
		
		return NONE;
	}
}
